package com.aletto.general_store.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.OrderItem;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.utils.XHelper;
import com.aletto.general_store.utils.image.GlideApp;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.ToastView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.Serializable;

/**
 * 评论
 */
public class OrderCommentActivity extends AppCompatActivity {

    public static Intent newIntent(Context context, OrderItem item) {
        Intent intent = new Intent(context, OrderCommentActivity.class);
        intent.putExtra("ITEM", item);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_comment);

        Intent intent = getIntent();
        OrderItem item = (OrderItem) intent.getSerializableExtra("ITEM");

        XHelper.initTitle(this, R.string.title_order_comment);

        ImageView iv_goods_cover = findViewById(R.id.iv_goods_cover);
        TextView tv_goods_title = findViewById(R.id.tv_goods_title);
        RatingBar ratingBar = findViewById(R.id.rating_bar);
//        CheckBox tv_anonymous_comment = findViewById(R.id.tv_anonymous_comment);
        TextView confirm_button = findViewById(R.id.confirm_button);

        tv_goods_title.setText(item.getGoods_name());
        GlideApp.with(iv_goods_cover).load(item.getGoods_img().get(0)).normal().into(iv_goods_cover);

        confirm_button.setOnClickListener(v -> task(item.getOrderId(), ratingBar.getNumStars()).execute(getLifecycle()));
    }


    private SimpleTemplate<Object> task(long orderId, int score){
        return new SimpleTemplate<>() {
            @Override
            public INetTask.View<Object> view() {
                return new ToastView<>(getApplication()){
                    @Override
                    public void success(Object data) {
                        finish();
                    }
                };
            }

            @Override
            public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
                return new NetSingleTask<>(DataRepository.getInstance().orderRating(orderId, score));
            }
        };
    }

}
